package com.prj.ufdm.cloud.ms.test.bpo._impl.qcc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prj.ufdm.cloud.ms.core.util.CoreRestTemplateUtil;
import com.prj.ufdm.cloud.ms.test.bpo.jsontable.FastJsonRecordBPO;
import com.prj.ufdm.cloud.ms.test.bpo.jsontable.FastJsonTableBPO;
import com.prj.ufdm.cloud.ms.test.bpo.qcc.QccBPO;
import com.prj.ufdm.cloud.ms.test.constants.MsTestConstants;
import com.prj.ufdm.cloud.ms.test.model.jsontable.FastJsonTable;
import com.prj.ufdm.cloud.ms.test.util.DbTableUtils;
import com.prj.ufdm.cloud.ms.test.util.FastJsonUtils;

/**
 * DESC: 企查查接口服务实现类 <br>
 * @author 胡义振
 * @date 2020年3月17日
 */
@Service
public class QccBPOImpl implements QccBPO {

	@Autowired
	DbTableUtils dbTableUtils;
	@Autowired
	FastJsonTableBPO fastJsonTableBPO;
	@Autowired
	FastJsonRecordBPO fastJsonDmlBPO;
	
	/**
	 * DESC: 执行查询
	 * @auther 胡义振
	 * @date 2020年3月7日
	 * @param reqUrl
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 * @return
	 */
	@Override
	public String executeQccQuery(String reqUrl, Map<String, String> paramMap) throws Exception{
		Map<String,String> reqHeadParamMap = new HashMap<String,String>();
		String[] autherHeader = RandomAuthentHeader();
		reqHeadParamMap.put("Token", autherHeader[0]);
		reqHeadParamMap.put("Timespan", autherHeader[1]);
		Map<String,String> queryParamMap = new HashMap<String,String>();
		queryParamMap.put("key", MsTestConstants.QICHACHA_APP_KEY);
		
		if(paramMap!=null) {
			for(Map.Entry<String,String> entry : paramMap.entrySet()){
		        String key = entry.getKey();
		        String value = paramMap.get(key);
		        queryParamMap.put(key, value);
			}
		}
		CoreRestTemplateUtil restTmpl = new CoreRestTemplateUtil(new RestTemplate());
		String reqResult = restTmpl.reqGetParam(reqUrl, reqHeadParamMap, queryParamMap, null);
		return reqResult;
	}

	
	/**
	 * DESC: 获取Token与Timespan
	 * @auther 胡义振
	 * @date 2020年2月28日
	 * @return 
	 * @return
	 */
	private static final String[] RandomAuthentHeader() {
		String timeSpan = String.valueOf(System.currentTimeMillis() / 1000);
		String[] authentHeaders = new String[] { DigestUtils.md5Hex(MsTestConstants.QICHACHA_APP_KEY.concat(timeSpan).concat(MsTestConstants.QICHACHA_SEC_KEY)).toUpperCase(), timeSpan };
		return authentHeaders;
	}


	@Override
	@Async
	public void saveQueryResult(String queryResult,String rootTableName) throws Exception {
		JSONObject qxxJsonObject = JSON.parseObject(queryResult);
		Object gsMasterObject = qxxJsonObject.get("Result");
		// FastJson 转为 List<FastJsonTable>
		List<FastJsonTable>  fastJsonTableList = FastJsonUtils.doGetJsonTable(null,gsMasterObject, rootTableName,null);
		System.out.println("\n fastJsonTableList:"+fastJsonTableList);
		// 表结构操作
		String operateTableSql = fastJsonTableBPO.operateTableSql(fastJsonTableList);
		dbTableUtils.executeSql(operateTableSql);
		System.out.println("\n operateTableSql:"+operateTableSql);
		// 数据操作
		String insertRecordSql = fastJsonDmlBPO.createInsertRecordSql(fastJsonTableList);
		dbTableUtils.executeSql(insertRecordSql);
		System.out.println("\n insertRecordSql:"+insertRecordSql);
	}
	
}
